""""""""""""""""""""""""""""""""""""""
" 语法着色与高亮设置
""""""""""""""""""""""""""""""""""""""

"开启语法高亮
syntax enable
syntax on

"配色
colo ruki_dark
colors ruki_dark

"设置高亮搜索
:set hlsearch

""""""""""""""""""""""""""""""""""""""
" 文件设置
""""""""""""""""""""""""""""""""""""""
"set autochdir
set encoding=utf-8
set fileencoding=chinese
set fileencodings=ucs-bom,utf-8,chinese 
set ambiwidth=double
set langmenu=zh_CN.UTF-8
set imcmdline
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim

"检测文件的类型
filetype on 

"默认无备份
:set nobackup
:set nowritebackup

""""""""""""""""""""""""""""""""""""""
" 鼠标设置
""""""""""""""""""""""""""""""""""""""

"鼠标支持
if has('mouse')
:set mouse=a
endif

"使鼠标用起来象微软 Windows,似乎正好解决 连续多行缩进问题、退格问题
:behave mswin

""""""""""""""""""""""""""""""""""""""
" 编辑器设置
""""""""""""""""""""""""""""""""""""""

"显示行号
set number

"Tab 宽度
:set ts=4
:set expandtab

"自动缩进
:set sw=4

"C/C++ 风格的自动缩进
:set cin
"设定 C/C++ 风格自动缩进的选项
:set cino=:0g0t0(sus

"打开普通文件类型的自动缩进
:set ai

"在编辑过程中，在右下角显示光标位置的状态行
:set ruler

"显示匹配括号
:set showmatch

"在insert模式下能用删除键进行删除
:set backspace=indent,eol,start

"代码折叠, 命令 za
:set foldmethod=syntax
:set foldlevel=100  "启动vim时不要自动折叠代码

"设置字体 
:set guifont=courier\ new:h14

"当右键单击窗口的时候， 弹出快捷菜单
:set mousemodel=popup

" nowrap
:set nowrap
if (has("gui_running"))	

" add scroll bar
:set guioptions+=b

endif

""""""""""""""""""""""""""""""""""""""
" 快捷键设置
""""""""""""""""""""""""""""""""""""""
" replace tab to spaces
map <F4> :%retab!<CR>

" fix paste bug for vim + tmux
set clipboard=unnamed

" copy, paste and cute: ctrl+c ctrl+v ctrl+x
:map <C-C> y
:map <C-X> x
:map <C-Z> u

" paste from clipboard when in insert mode.
imap <C-V> <ESC>"_dP
" paste from clipboard when in visual mode. (Replace whatever is selected in visual mode.)
" fix some clipboard bugs, @see https://stackoverflow.com/questions/290465/how-to-paste-over-without-overwriting-register
vmap <C-V> "_dP

" select all: ctrl+a
:map <C-A> ggVG
:map! <C-A> <Esc>ggVG

" delete and backspace
vnoremap <BS> "_d
nnoremap <BS> "_d
vnoremap <Del> "_d
nnoremap <Del> "_d

" redo
:nmap r <c-r>

" 多行缩进
:map <Tab> >
:map <S-Tab> <

" 映射 git mergetool -t gvimdiff
map <silent> <leader>1 :diffget <CR> 
map <silent> <leader>2 :diffget 2<CR>
map <silent> <leader>3 :diffget 3<CR>
map <silent> <leader>4 :diffget 4<CR>

""""""""""""""""""""""""""""""""""""""
" 插件设置
""""""""""""""""""""""""""""""""""""""

"开启OmniCppComplete.vim
set nocp
filetype plugin on

"2Html插件，启用XHtml css
:let html_number_lines=1
:let html_use_css=1
:let use_xhtml=1

"fencview.vim 插件设置
let g:fencview_autodetect = 1  "打开文件时自动识别编码
let g:fencview_checklines = 10 "检查前后10行来判断编码

"autocomplpop.vim & supertab.vim 插件设置
let g:AutoComplPop_IgnoreCaseOption=1
set ignorecase

" tagbar
let g:tagbar_left = 1
let g:tagbar_sort = 0
let g:tagbar_autofocus = 1
let g:tagbar_expand = 1
let g:tagbar_singleclick = 1
map <silent> <leader>s :TagbarToggle<CR>

let g:tagbar_type_scala = {
    \ 'ctagstype' : 'scala',
    \ 'sro'       : '.',
    \ 'kinds'     : [
      \ 'p:packages',
      \ 'T:types:1',
      \ 't:traits',
      \ 'o:objects',
      \ 'O:case objects',
      \ 'c:classes',
      \ 'C:case classes',
      \ 'm:methods',
      \ 'V:values:1',
      \ 'v:variables:1'
    \ ]
\ }

let g:tagbar_type_go = {
    \ 'ctagstype': 'go',
    \ 'kinds' : [
        \'p:package',
        \'f:function',
        \'v:variables',
        \'t:type',
        \'c:const'
    \]
\}

let g:tagbar_type_d = {
            \ 'ctagstype' : 'd',
            \ 'kinds'     : [
            \ 'c:classes:1:1',
            \ 'f:functions:1:1',
            \ 'T:template:1:1',
            \ 'g:enums:1:1',
            \ 'e:enumerators:0:0',
            \ 'u:unions:1:1',
            \ 's:structs:1:1',
            \ 'v:variables:1:0',
            \ 'i:interfaces:1:1',
            \ 'm:members',
            \ 'a:alias'
            \ ],
            \'sro': '.',
            \ 'kind2scope' : {
            \ 'c' : 'class',
            \ 'g' : 'enum',
            \ 's' : 'struct',
            \ 'u' : 'union',
            \ 'T' : 'template'
            \},
            \ 'scope2kind' : {
            \ 'enum'      : 'g',
            \ 'class'     : 'c',
            \ 'struct'    : 's',
            \ 'union'     : 'u',
            \ 'template'  : 'T'
            \ },
            \ 'ctagsbin' : 'dscanner',
            \ 'ctagsargs' : ['--ctags']
            \ }

" Add support for markdown files in tagbar.
let g:tagbar_type_markdown = {
    \ 'ctagstype': 'markdown',
    \ 'ctagsbin' : '~/.vim/markdown2ctags.py',
    \ 'ctagsargs' : '-f - --sort=yes',
    \ 'kinds' : [
        \ 's:sections',
        \ 'i:images'
    \ ],
    \ 'sro' : '|',
    \ 'kind2scope' : {
        \ 's' : 'section',
    \ },
    \ 'sort': 0,
\ }

" add a definition for Objective-C to tagbar
let g:tagbar_type_objc = {
    \ 'ctagstype' : 'ObjectiveC',
    \ 'kinds'     : [
        \ 'i:interface',
        \ 'I:implementation',
        \ 'p:Protocol',
        \ 'm:Object_method',
        \ 'c:Class_method',
        \ 'v:Global_variable',
        \ 'F:Object field',
        \ 'f:function',
        \ 'p:property',
        \ 't:type_alias',
        \ 's:type_structure',
        \ 'e:enumeration',
        \ 'M:preprocessor_macro',
    \ ],
    \ 'sro'        : ' ',
    \ 'kind2scope' : {
        \ 'i' : 'interface',
        \ 'I' : 'implementation',
        \ 'p' : 'Protocol',
        \ 's' : 'type_structure',
        \ 'e' : 'enumeration'
    \ },
    \ 'scope2kind' : {
        \ 'interface'      : 'i',
        \ 'implementation' : 'I',
        \ 'Protocol'       : 'p',
        \ 'type_structure' : 's',
        \ 'enumeration'    : 'e'
    \ }
\ }

let g:tagbar_type_rust = {
    \ 'ctagstype' : 'rust',
    \ 'kinds' : [
        \'T:types,type definitions',
        \'f:functions,function definitions',
        \'g:enum,enumeration names',
        \'s:structure names',
        \'m:modules,module names',
        \'c:consts,static constants',
        \'t:traits,traits',
        \'i:impls,trait implementations',
    \]
    \}

let g:tagbar_type_mql4 = {
    \ 'ctagstype': 'C++',
    \ 'kinds' : [
        \'f:function',
        \'v:variables',
        \'t:type',
        \'c:const'
    \]
\}

" NERDTree
let g:NERDTreeWinPos="right"
let g:NERDTreeIgnore=['\.o$', '\~$', '\.a$', '\.swp$', '\.so$', '\.b$', '\.bak$', '\.out$']
let g:NERDChristmasTree=1
let g:NERDTreeAutoCenter=1
let g:NERDTreeQuitOnOpen=1
map <silent> <leader>t :NERDTreeToggle<CR>

" cscope
if has("cscope")
	set csprg=/usr/bin/cscope
	set csto=0
	set cst
	set nocsverb
	" add any database in current directory
	if filereadable("cscope.out")
		cs add cscope.out
	" else add database pointed to by environment
	elseif $CSCOPE_DB != ""
		cs add $CSCOPE_DB
	endif
	set csverb
endif
map <silent> <leader>g <C-]>
map <silent> <leader>r <C-t><CR>

" ShowMarks
let showmarks_enable = 1
" Show which marks
let showmarks_include = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
" Ignore help, quickfix, non-modifiable buffers
let showmarks_ignore_type = "hqm"
" Hilight lower & upper marks
let showmarks_hlline_lower = 1
let showmarks_hlline_upper = 1 

" MarksBrowser
map <silent><leader>ml :MarksBrowser<CR> 

" a.vim
map <silent><leader>h :A<CR>

" EasyAlign
" Start interactive EasyAlign in visual mode (e.g. vipga)
xmap ga <Plug>(EasyAlign)
" Start interactive EasyAlign for a motion/text object (e.g. gaip)
nmap ga <Plug>(EasyAlign)

" lightline
if !has('gui_running')
    set t_Co=256
endif
set laststatus=2

let g:Powerline_symbols = 'fancy'
let g:lightline = {
      \ 'colorscheme': 'wombat',
      \ 'active': {
      \   'left': [ [ 'mode', 'paste' ],
      \             [ 'fugitive', 'readonly', 'filename', 'modified' ] ]
      \ },
      \ 'component': {
      \   'readonly': '%{&filetype=="help"?"":&readonly?"x":""}',
      \   'modified': '%{&filetype=="help"?"":&modified?"+":&modifiable?"":"-"}',
      \   'fugitive': '%{exists("*fugitive#head")?fugitive#head():""}'
      \ },
      \ 'component_visible_condition': {
      \   'readonly': '(&filetype!="help"&& &readonly)',
      \   'modified': '(&filetype!="help"&&(&modified||!&modifiable))',
      \   'fugitive': '(exists("*fugitive#head") && ""!=fugitive#head())'
      \ },
      \ 'separator': { 'left': '', 'right': '' },
      \ 'subseparator': { 'left': '|', 'right': '|' }
      \ }

" treat all numerals as decimal not octal 
set nrformats=

" wildfire (smart selection)
let g:wildfire_objects = ["iw", "i'", 'i"', "i)", "i]", "i}", "ip", "it"]
let g:wildfire_fuel_map = "<Enter>"
let g:wildfire_water_map = "<Space>"

" leaderf
let g:Lf_CommandMap = {'<C-K>': ['<Up>'], '<C-J>': ['<Down>']}

""""""""""""""""""""""""""""""""""""""
" 其他设置
""""""""""""""""""""""""""""""""""""""

"去掉讨厌的有关vi一致性模式，避免以前版本的一些bug和局限
:set nocompatible

""""""""""""""""""""""""""""""""""""""
" neovim
""""""""""""""""""""""""""""""""""""""

" terminal
if has('nvim')
    map <silent> <leader>- :bo split \|resize 12\|term bash --login<CR>
    map <silent> <leader>= :vertical rightbelow split\|term bash --login<CR>
endif

